#!/bin/sh
#
# Copyright 2009 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

set -e


# Add icons to the system icons
XDG_ICON_RESOURCE="`command -v xdg-icon-resource 2> /dev/null || true`"
if [ ! -x "$XDG_ICON_RESOURCE" ]; then
  echo "Error: Could not find xdg-icon-resource" >&2
  exit 1
fi
for icon in product_logo_32.png product_logo_24.png product_logo_128.png product_logo_16.png product_logo_48.png product_logo_256.png product_logo_64.png ; do
  size="$(echo ${icon} | sed 's/[^0-9]//g')"
  "$XDG_ICON_RESOURCE" install --size "${size}" "/opt/microsoft/msedge/${icon}" \
    "microsoft-edge"
done

UPDATE_MENUS="`command -v update-menus 2> /dev/null || true`"
if [ -x "$UPDATE_MENUS" ]; then
  update-menus
fi

# Update cache of .desktop file MIME types. Non-fatal since it's just a cache.
update-desktop-database > /dev/null 2>&1 || true

# Updates defaults.list file if present.
update_defaults_list() {
  # $1: name of the .desktop file

  local DEFAULTS_LIST="/usr/share/applications/defaults.list"

  if [ ! -f "${DEFAULTS_LIST}" ]; then
    return
  fi

  # Split key-value pair out of MimeType= line from the .desktop file,
  # then split semicolon-separated list of mime types (they should not contain
  # spaces).
  mime_types="$(grep MimeType= /usr/share/applications/${1} |
                cut -d '=' -f 2- |
                tr ';' ' ')"
  for mime_type in ${mime_types}; do
    if egrep -q "^${mime_type}=" "${DEFAULTS_LIST}"; then
      if ! egrep -q "^${mime_type}=.*${1}" "${DEFAULTS_LIST}"; then
        default_apps="$(grep ${mime_type}= "${DEFAULTS_LIST}" |
                        cut -d '=' -f 2-)"
        egrep -v "^${mime_type}=" "${DEFAULTS_LIST}" > "${DEFAULTS_LIST}.new"
        echo "${mime_type}=${default_apps};${1}" >> "${DEFAULTS_LIST}.new"
        mv "${DEFAULTS_LIST}.new" "${DEFAULTS_LIST}"
      fi
    else
      # If there's no mention of the mime type in the file, add it.
      echo "${mime_type}=${1};" >> "${DEFAULTS_LIST}"
    fi
  done
}

update_defaults_list "microsoft-edge.desktop"

# This function uses sed to insert the contents of one file into another file,
# after the first line matching a given regular expression. If there is no
# matching line, then the file is unchanged.
insert_after_first_match() {
  # $1: file to update
  # $2: regular expression
  # $3: file to insert
  sed -i -e "1,/$2/ {
    /$2/ r $3
    }" "$1"
}

# If /usr/share/gnome-control-center/default-apps/gnome-default-applications.xml
# exists, it may need to be updated to add ourselves to the default applications
# list. If we find the file and it does not seem to contain our patch already
# (the patch is safe to leave even after uninstall), update it.
GNOME_DFL_APPS=/usr/share/gnome-control-center/default-apps/gnome-default-applications.xml
if [ -f "$GNOME_DFL_APPS" ]; then
# Conditionally insert the contents of the file "default-app-block" after the
# first "<web-browsers>" line we find in gnome-default-applications.xml
  fgrep -q "Microsoft Edge" "$GNOME_DFL_APPS" || insert_after_first_match \
    "$GNOME_DFL_APPS" \
    "^[ 	]*<web-browsers>[ 	]*$" \
    "/opt/microsoft/msedge/default-app-block"
fi

# Add to the alternatives system
#
# On Ubuntu 12.04, we have the following priorities
# (which can be obtain be installing browsers and running
# update-alternatives --query x-www-browser):
#
# /usr/bin/epiphany-browser  85
# /usr/bin/firefox           40
# /usr/bin/konqueror         30
#
# While we would expect these values to be keyed off the most popular
# browser (Firefox), in practice, we treat Epiphany as the lower bound,
# resulting in the following scheme:

CHANNEL=stable
case $CHANNEL in
  stable )
    # Good enough to be the default.
    PRIORITY=200
    ;;
  beta )
    # Almost good enough to be the default. (Firefox stable should arguably be
    # higher than this, but since that's below the "Epiphany threshold", we're
    # not setting our priority below it. Anyone want to poke Firefox to raise
    # their priority?)
    PRIORITY=150
    ;;
  unstable )
    # Unstable, give it the "lowest" priority.
    PRIORITY=120
    ;;
  * )
    PRIORITY=0
    ;;
esac

update-alternatives --install /usr/bin/x-www-browser x-www-browser \
  /usr/bin/microsoft-edge-stable $PRIORITY
update-alternatives --install /usr/bin/gnome-www-browser gnome-www-browser \
  /usr/bin/microsoft-edge-stable $PRIORITY

update-alternatives --install /usr/bin/microsoft-edge microsoft-edge \
  /usr/bin/microsoft-edge-stable $PRIORITY

DEFAULT_ARCH="amd64"

get_lib_dir() {
  if [ "$DEFAULT_ARCH" = "i386" ]; then
    LIBDIR=lib/i386-linux-gnu
  elif [ "$DEFAULT_ARCH" = "amd64" ]; then
    LIBDIR=lib/x86_64-linux-gnu
  elif [ "$DEFAULT_ARCH" = "armhf" ]; then
    LIBDIR=lib/arm-linux-gnueabihf
  elif [ "$DEFAULT_ARCH" = "arm64" ]; then
    LIBDIR=lib/aarch64-linux-gnu
  elif [ "$DEFAULT_ARCH" = "mipsel" ]; then
    LIBDIR=lib/mipsel-linux-gnu
  elif [ "$DEFAULT_ARCH" = "mips64el" ]; then
    LIBDIR=lib/mips64el-linux-gnuabi64
  else
    echo Unknown CPU Architecture: "$DEFAULT_ARCH"
    exit 1
  fi
}

NSS_FILES="libnspr4.so.0d libplds4.so.0d libplc4.so.0d libssl3.so.1d \
    libnss3.so.1d libsmime3.so.1d libnssutil3.so.1d"

add_nss_symlinks() {
  get_lib_dir
  for f in $NSS_FILES
  do
    target=$(echo $f | sed 's/\.[01]d$//')
    if [ -f "/$LIBDIR/$target" ]; then
      ln -snf "/$LIBDIR/$target" "/opt/microsoft/msedge/$f"
    elif [ -f "/usr/$LIBDIR/$target" ]; then
      ln -snf "/usr/$LIBDIR/$target" "/opt/microsoft/msedge/$f"
    else
      echo $f not found in "/$LIBDIR/$target" or "/usr/$LIBDIR/$target".
      exit 1
    fi
  done
}

remove_nss_symlinks() {
  for f in $NSS_FILES
  do
    rm -rf "/opt/microsoft/msedge/$f"
  done
}

remove_udev_symlinks() {
  rm -rf "/opt/microsoft/msedge/libudev.so.0"
}

remove_udev_symlinks

